package com.chaoyer.cynet.common.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.FullHttpRequest;
import org.apache.commons.lang3.StringUtils;

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


/**
 * 通用工具类
 */
public class CommonUtils {

    /**
     * 生成uuid
     */
    public static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }


    /**
     * @param obj fastjson转化
     * @return json
     */
    public static Object filterJson(Object obj) {

        String s = JSONObject.toJSONString(obj,
                SerializerFeature.PrettyFormat,
//                SerializerFeature.WriteMapNullValue,//保留空的字段
//                SerializerFeature.WriteNullStringAsEmpty,//String null -> ""
//                SerializerFeature.WriteNullNumberAsZero,//Number null -> 0
                SerializerFeature.WriteDateUseDateFormat,
                SerializerFeature.WriteNullListAsEmpty)
//                SerializerFeature.WriteNullBooleanAsFalse)
                ;

        Object result;
        try {
            result = JSONObject.parseObject(s);
        } catch (ClassCastException e) {
            result = JSONArray.parse(s);
        }
        return result;
    }


    /**
     * 客户端ip   仅限netty中用
     *
     * @param ctx     ChannelhandleContext
     * @param request http请求
     * @return 客户端ip
     */
    public static String getRemoteIP(ChannelHandlerContext ctx, FullHttpRequest request) {
        String ip = request.headers().get("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.headers().get("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.headers().get("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.headers().get("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.headers().get("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            InetSocketAddress inetSocketAddress = (InetSocketAddress) ctx.channel().remoteAddress();
            ip = inetSocketAddress.getAddress().getHostAddress();
        }
        // 如果是多级代理，那么取第一个ip为客户端ip
        if (ip != null && ip.contains(",")) {
            ip = ip.substring(0, ip.indexOf(",")).trim();
        }

        return ip;
    }


    /**
     * 对象转Integer
     *
     * @param obj 强转的对象
     * @return Integer对象
     */
    public static Integer objectToInt(Object obj) {
        if (obj == null) {
            return null;
        }
        if (obj instanceof Integer) {
            return (Integer) obj;
        }

        return Integer.valueOf((String) obj);
    }

    /**
     * List<Object> to List<String>
     * 默认保留空项
     *
     * @param list List<Object>
     * @return List<String>
     */
    public static List<String> olistToStrList(List<Object> list) {
        return olistToStrList(list, false);
    }

    /**
     * List<Object> to List<String>
     *
     * @param list     List<Object>
     * @param disEmpty 是否要忽略空项
     * @return List<String>
     */
    public static List<String> olistToStrList(List<Object> list, boolean disEmpty) {
        if (list == null) {
            return null;
        }
        List<String> result = new ArrayList<>();
        for (Object aList : list) {
            if (aList instanceof String) {
                String s = (String) aList;
                if (!disEmpty || StringUtils.isNotBlank(s)) {
                    result.add(s);
                }
            }
        }
        return result;
    }

}
